/*
* TCSS 422, Project 2 (Scheduler Simulation)
* Team 9 Name: blueraccoon422
* Team 9 Group Members: Jacob Hall, Jacob Mertens, Diana Gorun, Alex Stringham
* File Name: Main.c
* Date: May 22, 2013
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include "CPU.h"
#include "Process.h"
/*
 * Command line invocation: $./scheduler -p 22 -k 2 -io 3 -pc 3 -c 10000
 *
 * The number of total processes cannot be exceeded by the number of k, io,
 * pc processes.
 *
 * This specifies a total of 22 processes, two of which will be keyboard,
 * three will be I/O-bound, there will be 3 pairs of producers-consumers,
 * and the rest will be compute-bound.  -c specifies the number of cycles
 * to run the CPU before stopping.
 */

static int cpu_cycles;
static int num_total_proc;
static int num_keybd_proc;
static int num_io_proc;
static int num_pc_proc;

/*
 * Method sets up the parameters for the numbers and types of processes
 * specified by the user.
 */
int parseUserInput(int argc, char *argv[])
{
	int n;
	for (n = 1; n < argc; n = n + 2) {
		if (strcmp(argv[n], "-p") == 0) {
			if (isdigit(*argv[n + 1])) {
				printf("Number of total processes: %s\n", argv[n + 1]);
				num_total_proc = atoi(argv[n + 1]);
			} else {
				puts("ERROR: Did not specify a number of total processes.");
				return 0;
			}
		} else if (strcmp(argv[n], "-k") == 0) {
			printf("Number of Keyboard processes: %s\n", argv[n + 1]);
			num_keybd_proc = atoi(argv[n + 1]);
		} else if (strcmp(argv[n], "-io") == 0) {
			printf("Number of I/O processes: %s\n", argv[n + 1]);
			num_io_proc = atoi(argv[n + 1]);
		} else if (strcmp(argv[n], "-pc") == 0) {
			printf("Number of pairs of producer-consumer processes: %s\n",
					argv[n + 1]);
			//num_pc_proc = atoi(argv[n + 1]);
			num_pc_proc = 2;
		} else if (strcmp(argv[n], "-c") == 0) {
			printf("Number of Cycles to run: %s\n", argv[n + 1]);
			//cpu_cycles = atoi(argv[n + 1]);	//convert Char[] to int, store as cpu_cycles
			cpu_cycles = 3;
		} else {
			printf("Unrecognized command line arguments.\n"
					"Must be:\n"
					"-p  ## for total processes\n"
					"-k  ## for keyboard processes\n"
					"-io ## for input/output processes\n"
					"-pc ## for producer/consumer processes.\n"
					"-c  ## for the number of cycles to run.\n");
			return 0;
		}
	}
	if (num_total_proc < (num_keybd_proc + num_io_proc + num_pc_proc)) {
		puts("\n*****************************************************\n"
				"The number of processes input exceed the total number\n"
				"specified for the value '-p'.  Please re-enter data.\n"
				"****************************************************\n\n");
	}
	return 0;
}


int main( int argc, char *argv[] )
{
	parseUserInput(argc, argv);
	cpu_cycles = 10000;
	num_total_proc = 5;
	num_keybd_proc = 2;
	num_io_proc = 1;
	num_pc_proc = 2;

	createCPUProcessor(cpu_cycles, num_total_proc, num_keybd_proc,
			num_io_proc, num_pc_proc);

	//TODO remove puts
	printf("DONE!");

	return 0;
}

